package View;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class AddStudent extends JFrame {
    JFrame jf=new JFrame("添加学生");
    JTextField t1=new JTextField(15);
    JTextField t2=new JTextField(15);
    JTextField t3=new JTextField(15);
    JTextField t4=new JTextField(15);
    JLabel ts=new JLabel();
    public AddStudent(){
        Box b=Box.createVerticalBox();
        Box b1=Box.createHorizontalBox();
        JLabel j1=new JLabel("学号");

        b1.add(j1);
        b1.add(t1);
        b.add(b1);

        Box b2=Box.createHorizontalBox();
        JLabel j2=new JLabel("密码");

        b2.add(j2);
        b2.add(t2);
        b.add(b2);

        Box b3=Box.createHorizontalBox();
        JLabel j3=new JLabel("姓名");

        b3.add(j3);
        b3.add(t3);
        b.add(b3);

        Box b4=Box.createHorizontalBox();
        JLabel j4=new JLabel("所选课程");

//        b4.add(j4);
//        b4.add(t4);
//        b.add(b4);

        Box b5=Box.createHorizontalBox();
        JButton j5=new JButton("确认添加");
        j5.addActionListener(new tianjiaJT());
        b5.add(j5);
        b.add(b5);

        Box b6=Box.createHorizontalBox();
        b6.add(ts);
        b.add(b6);

        jf.add(b);
        jf.setSize(400,155);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        jf.setResizable(false);
        jf.setVisible(true);
        jf.setLayout(new FlowLayout());


    }

    public static void main(String[] args) {
        new AddStudent();
    }
    public class tianjiaJT implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String xuehao=t1.getText();
            String mima=t2.getText();
            String xingming=t3.getText();
            String kecheng=t4.getText();
           if (xuehao.equals("")||mima.equals("")||xingming.equals("")){
               ts.setText("学生基本信息不能为空");
           }else {
               try {
                   Class.forName("com.mysql.cj.jdbc.Driver");
                   Connection c= DriverManager.getConnection("jdbc:mysql://localhost:3306/lxt","root","111111");
                   Statement s=c.createStatement();
                   ResultSet result=s.executeQuery("select *from evaluate where 学号='" + xuehao + "'");
                   if (result.next()){
                       ts.setText("此学号已存在");
                   }else {
                       s.executeUpdate("insert into evaluate(学号,密码,姓名)values('"+xuehao+"','"+mima+"','"+xingming+"')");
                       ts.setText("添加成功");
                       new ZhuJieMian().setPagnow(100);
                       ZhuJieMian.reloadTable();

                   }

//                   if (result.next()){
////                    System.out.println(result.getInt("id")+result.getString("user")+result.getString("ps"));
//                       ResultSet r=s.executeQuery("select *from 注册表 where password='"+mi+"'");
//                       if (r.next()){
//                           new ZhuJieMian().into();
//                           jf.dispose();
//                       }
                   System.out.println(result);
//                   }

                   result.close();
                   s.close();
                   c.close();

               } catch (SQLException throwables) {
                   throwables.printStackTrace();
               } catch (ClassNotFoundException classNotFoundException) {
                   classNotFoundException.printStackTrace();
               }

           }
//           new ZhuJieMian().into();
//           jf.dispose();
        }

    }
}
